package com.masterclient.app.modules.timelimited;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.utils.status.StatusBarUtil;
import com.eoner.baselib.widget.empty.FCPageStateView;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.TimeUtil;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.timelimited.adapter.ViewPagerAdapter;
import com.masterclient.app.modules.timelimited.contract.TimeLimitedTabContract;
import com.masterclient.app.modules.timelimited.contract.TimeLimitedTabContractPresenter;
import com.masterclient.app.modules.timelimited.model.TimeLimitedTab;
import com.masterclient.app.modules.timelimited.model.TimeTabData;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class TimeLimitedBuyActivity extends BaseActivity<TimeLimitedTabContractPresenter> implements TimeLimitedTabContract.View {

    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @BindView(R.id.image)
    ImageView image;


    @BindView(R.id.empty_view)
    FCPageStateView empty_layout;
    @BindView(R.id.ll_content)
    View ll_content;


    private List<Fragment> fragments;
    private  TimeLimitedTab timeLimitedTab;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time_limited_buy);
        ButterKnife.bind(this);

        StatusBarUtil.setStatusBarColor(getActivity(), Color.parseColor("#FFFFFF"));
        empty_layout.setViewState(FCPageViewState.PAGE_STATE_NO_DATA,R.mipmap.icon_empty_limit,"最近暂无新活动，敬请期待活动上新通知");
        empty_layout.setVisibility(View.GONE);

        init();
        addListener();
        getTab();
    }

    private void addListener() {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                set_time_tab(position, true);
                viewPager.setCurrentItem(position);
            }


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void getTab() {
        mPresenter.getTabData();
    }

    private void init() {
        intentSource = getBundleString("intentSource");
        fragments = new ArrayList<>();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case 103:


                break;
            case 104:

                break;
        }
    }

    @OnClick({R.id.rl_back})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_tixian:

                break;
            case R.id.exchange_btn:

                break;

        }
    }

    @Override
    public void showData(TimeLimitedTab timeLimitedTab) {
        this.timeLimitedTab = timeLimitedTab;
        tabLayout.removeAllTabs();
        if(timeLimitedTab.getSh_list() != null && timeLimitedTab.getSh_list().size() >0){
            empty_layout.setVisibility(View.GONE);
            ll_content.setVisibility(View.VISIBLE);
            int select = -1;//默认选中的位置
            GlideUtil.displayImage(mContext,timeLimitedTab.getSh_banner(),image,R.mipmap.defaults_1);
//            if(!TextUtils.isEmpty(timeLimitedTab.getSh_banner())){
//                App.picasso.load(timeLimitedTab.getSh_banner()).into(image);
//            }
            for (int i = 0; i < timeLimitedTab.getSh_list().size(); i++) {
                tabLayout.addTab(tabLayout.newTab());
                TabLayout.Tab tab = tabLayout.getTabAt(i);
                TimeTabData timeTabData = timeLimitedTab.getSh_list().get(i);
                if (tab != null) {
                    tab.setCustomView(R.layout.home_time_tab_first_item);
                    View view = tab.getCustomView();
                    TextView startTime = (TextView) view.findViewById(R.id.startTime);
                    TextView status = (TextView) view.findViewById(R.id.status);
                    View ll_state_bg = view.findViewById(R.id.ll_state_bg);
                    // TODO 根据状态设置背景颜色和字体颜色
                    startTime.setText(timeTabData.getSh_title());
                    status.setText(timeTabData.getSh_status_title());
                    if ("1".equals(timeTabData.getSh_default_click())) {
                        select = i;
                    }
                }
                fragments.add(TimeLimitedBuyFragment.getInstance(timeTabData.getSh_location(),timeTabData.getSh_status()
                        ,timeTabData.getSh_lifetime(),timeTabData.getSh_event_map_id(),timeTabData.getSh_status_title(),
                        timeTabData.getSh_s_flashsale_starttime(),timeTabData.getSh_s_flashsale_endtime(),intentSource)) ;
            }
            if (select == -1) {
                select = 0;
            }

            if(fragments.size() > 4){
                tabLayout.setTabMode (TabLayout.MODE_SCROLLABLE);
                tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
            }else{
                tabLayout.setTabMode (TabLayout.MODE_SCROLLABLE);
                tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
            }

            ViewPagerAdapter mainPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),fragments);
            viewPager.setAdapter(mainPagerAdapter);
            //tabLayout.setupWithViewPager(viewPager);
            tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
            viewPager.setOffscreenPageLimit(fragments.size());
            if (tabLayout.getTabAt(select) != null) {
                tabLayout.getTabAt(select).select();
            }
            set_time_tab(select, false);
        }else{
            empty_layout.setVisibility(View.VISIBLE);
            ll_content.setVisibility(View.GONE);
        }

    }

    private void set_time_tab(int position, boolean isClick) {
        for (int i = 0; i < timeLimitedTab.getSh_list().size(); i++) {
            View view = tabLayout.getTabAt(i).getCustomView();
            if (view == null) {
                return;
            }
            TextView startTime = (TextView) view.findViewById(R.id.startTime);
            TextView status = (TextView) view.findViewById(R.id.status);
            View statusBg = (View) view.findViewById(R.id.ll_state_bg);
            if (position == i) {
                startTime.setTextColor(Color.parseColor("#FFFFFF"));
                status.setTextColor(Color.parseColor("#FFFFFF"));
                statusBg.setBackgroundColor(Color.parseColor("#212121"));
            } else {
                if (!TimeUtil.isBeforeNow(timeLimitedTab.getSh_list().get(i).getSh_s_flashsale_starttime())){
                    //未开始
                    startTime.setTextColor(Color.parseColor("#333333"));
                    status.setTextColor(Color.parseColor("#333333"));
                    statusBg.setBackgroundColor(Color.parseColor("#FFEBB3"));
                }
                if (TimeUtil.isBeforeNow(timeLimitedTab.getSh_list().get(i).getSh_s_flashsale_starttime()) &&
                        !TimeUtil.isBeforeNow(timeLimitedTab.getSh_list().get(i).getSh_s_flashsale_endtime())){
                    //开始
                    startTime.setTextColor(Color.parseColor("#333333"));
                    status.setTextColor(Color.parseColor("#333333"));
                    statusBg.setBackgroundColor(Color.parseColor("#FFEBB3"));
                }
                if (TimeUtil.isBeforeNow(timeLimitedTab.getSh_list().get(i).getSh_s_flashsale_endtime())){
                    //结束
                    startTime.setTextColor(Color.parseColor("#868D9D"));
                    status.setTextColor(Color.parseColor("#868D9D"));
                    statusBg.setBackgroundColor(Color.parseColor("#DCE3ED"));
                }
            }
        }
    }

    @Override
    public void loadFail(String s) {
        empty_layout.setVisibility(View.VISIBLE);
        ll_content.setVisibility(View.GONE);
    }

    @Override
    public void loadError(String s) {
        empty_layout.setVisibility(View.VISIBLE);
        ll_content.setVisibility(View.GONE);
    }
}
